package com.example.rlw.mvpplugintest.mvp;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.rlw.mvpplugintest.R;
import com.example.rlw.mvpplugintest.prompt.LoadingCallback;
import com.example.rlw.mvpplugintest.utils.ErrorCodeUtills;
import com.example.rlw.mvpplugintest.utils.NotificationUtils;
import com.jaeger.library.StatusBarUtil;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.noober.background.BackgroundLibrary;

import java.lang.reflect.ParameterizedType;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * MVPPlugin
 * Activity 基类
 */

public abstract class MVPBaseActivity<V extends BaseView, T extends BasePresenterImpl<V>> extends AppCompatActivity implements BaseView, Callback.OnReloadListener {
    public T mPresenter;
    private Unbinder mUnbinder;
    protected LoadService mLoadService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        BackgroundLibrary.inject(this);
        super.onCreate(savedInstanceState);
        int layoutId = getLayoutId();
        if (layoutId != 0) {
            setContentView(getLayoutId());
            mUnbinder = ButterKnife.bind(this);
        }
        //沉浸式状态栏
        StatusBarUtil.setColor(this, Color.parseColor("#3f51b5"));
        mPresenter = getInstance(this, 1);
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
        mLoadService = LoadSir.getDefault().register(this, this);
        mLoadService.showSuccess();
        initParms(getIntent().getExtras());
        initView();
        initData(savedInstanceState);
        initNet();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        mUnbinder.unbind();
    }

    /**
     * 初始化布局
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 初始化视图
     */
    protected abstract void initView();

    /**
     * 初始化数据
     *
     * @param savedInstanceState
     */
    protected abstract void initData(@Nullable Bundle savedInstanceState);

    /**
     * 网络请求
     */
    protected abstract void initNet();

    /**
     * 打开 activity 无参
     *
     * @param targetActivity
     */
    protected void openActivity(Class<?> targetActivity) {
        openActivity(targetActivity, null);
    }

    /**
     * 带参 跳转activity
     *
     * @param activity
     * @param bundle
     */
    protected void openActivity(Class<?> activity, Bundle bundle) {
        Intent intent = new Intent(this, activity);
        if (bundle != null) {
            intent.putExtra("bundle", bundle);
        }
        startActivity(intent);
    }

    /**
     * 接收上个页面传过来的值
     *
     * @param bundle
     */
    protected void initParms(Bundle bundle) {
    }

    @Override
    public Context getContext() {
        return this;
    }

    public <T> T getInstance(Object o, int i) {
        try {
            return ((Class<T>) ((ParameterizedType) (o.getClass()
                    .getGenericSuperclass())).getActualTypeArguments()[i])
                    .newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void showLoadingView() {
        mLoadService.showCallback(LoadingCallback.class);
    }

    @Override
    public void showSuccessView(String msg) {
        mLoadService.showSuccess();
    }

    @Override
    public void showFailureView(int code, String msg) {
        ErrorCodeUtills.disposeEorCode(code, msg, this, mLoadService);
    }

    @Override
    public void onReload(View v) {
        // 当重试时 需重写 该方法
    }

    @Override
    public void onProgress(long totalSize, long downSize) {
        int progress = (int) ((downSize / (double) totalSize) * 100);
        //更新进度条
        NotificationUtils.updateProgress(2, progress);
    }
}
